const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: '',
    title: '',
    orders: [],
    phoneInfo: {
      image: ''
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options);
    let _this = this;
    _this.setData({
      id: options.id,
      title: options.title
    });
    wx.request({
      url: 'https://www.xiaomutong.com.cn/web/index.php?r=orders/getorders', //仅为示例，并非真实的接口地址
      method: 'post',
      data: {
        projectid: options.id
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success (res) {
        console.log(res.data)
        let data = res.data;
        data.result.forEach(element => {
          console.log(element);
          element.nickname = decodeURIComponent(element.nickname);
          element.avatarUrl = JSON.parse(decodeURIComponent(element.userInfo)).avatarUrl;
        });
        _this.setData({
          orders: data.result
        });
      }
    });
    wx.request({
      url: 'https://www.xiaomutong.com.cn/web/index.php?r=phoneinfo/getphoneinfobyid', //仅为示例，并非真实的接口地址
      method: 'post',
      data: {
        id: options.id
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success (res) {
        console.log(res.data)
        let data = res.data;

        let phoneInfo = JSON.parse(res.data.result.phoneinfo);
        _this.setData({
          phone: data.result,
          phoneInfo: phoneInfo
        });
      }
    });  

    wx.login({
      success (res) {
        console.log(res);
        if (res.code) {
          //发起网络请求
          wx.request({
            url: 'https://www.xiaomutong.com.cn/web/index.php?r=wechat/getinfo',
            method: 'post',
            data: {
              code: res.code
            },
            success (res) {
              console.log(res.data);
              if(res.data && res.data.code == 0){
                let result = JSON.parse(res.data.result);
                console.log(result);
                _this.setData({
                  openid: result.openid
                })
              }
            },
            fail (err){
              console.log(err);
            }
          })
        } else {
          console.log('登录失败！' + res.errMsg)
        }
      }
    })
  },
  bindBorrow: function(){
    let _this = this;
    let _id = this.data.id;
    let _title = this.data.title;
    console.log(this.data);
    // 查看是否授权
    wx.getSetting({
      success (res){
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称
          wx.getUserInfo({
            success: function(res) {
              console.log(res.userInfo);
              app.globalData.userInfo = res.userInfo;
              _this.borrow(res.userInfo);
              
            }
          })
        }else{
          let url = '/pages/wechat/index?action=5&id='+_id +'&title=' +_title;
          wx.navigateTo({
            url: url
          })
        }
      }
    })
  },
  borrow: function(userInfo){
    let _this = this;
    let repeatable = this.data.repeatable;
    let openid = this.data.openid;
    let projectid = this.data.id;
    let title = this.data.title;
    console.log(this.data);
    wx.request({
      url: 'https://www.xiaomutong.com.cn/web/index.php?r=orders/addorder',
      method: 'post',
      data: {
        repeatable: repeatable,
        openid: openid,
        nickname:  encodeURIComponent(userInfo.nickName),
        projectid: projectid,
        title: title,
        userInfo: encodeURIComponent(JSON.stringify(userInfo))
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success (res) {
        console.log(res);
        if(res.data.code == 0){
          wx.showModal({
            showCancel: false,
            title: '提示',
            content: '参加成功',
            success (res) {
              if (res.confirm) {
                _this.getOrders();
              } else if (res.cancel) {
                console.log('用户点击取消')
              }
            }
          })
        }
        if(res.data.code == -1){
          wx.showModal({
            showCancel: false,
            title: '提示',
            content: '您已经参加',
            success (res) {
              if (res.confirm) {
                
              } else if (res.cancel) {
                console.log('用户点击取消')
              }
            }
          })
        }

      }
    });
  },
  getOrders: function(){
    let _this = this;
    let id = this.data.id;
    wx.request({
      url: 'https://www.xiaomutong.com.cn/web/index.php?r=orders/getorders', //仅为示例，并非真实的接口地址
      method: 'post',
      data: {
        projectid: id
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success (res) {
        console.log(res.data)
        let data = res.data;
        data.result.forEach(element => {
          console.log(element);
          element.nickname = decodeURIComponent(element.nickname);
          element.avatarUrl = JSON.parse(decodeURIComponent(element.userInfo)).avatarUrl;
        });
        _this.setData({
          orders: data.result
        });
      }
    });
  },
  bindCreateOrder: function(){
    let _id = this.data.id;
    let _title = this.data.title;
    // 查看是否授权
    wx.getSetting({
      success (res){
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称
          wx.getUserInfo({
            success: function(res) {
              console.log(res.userInfo);
              app.globalData.userInfo = res.userInfo;
              let url = '/pages/attend/index?id='+_id +'&title=' +_title;
              wx.navigateTo({
                url: url
              })
            }
          })
        }else{
          let url = '/pages/wechat/index?action=2&id='+_id +'&title=' +_title;
          wx.navigateTo({
            url: url
          })
        }
      }
    })
    
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  bindHome: function(){
    let url = '/pages/info/index';
    wx.switchTab({
      url: url
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})